package org.zpmis.api.http;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zpmis.api.business.OrderWebService;
import org.zpmis.common.base.BaseController;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.common.result.ResultCode;
import org.zpmis.domain.order.qo.*;

import javax.annotation.Resource;

/**
* created with CodeGenerator
* description: 订单处理控制类
* @author  Yang
* date: 2021-6-2
* time: 14:51:37
*/
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController extends BaseController{

    @Resource
    private OrderWebService orderWebService;


    /**
     * 申请订单任务
     * @param applyOrderQo
     * @return
     */
    @PostMapping("/applyOrder")
    public PlatformResult applyOrder(@Validated @RequestBody ApplyOrderQo applyOrderQo){
        try {
            orderWebService.applyOrder(applyOrderQo);
        } catch (BusinessException be) {
            log.error("[OrderController]---applyOrder--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        }catch (Exception e) {
            log.error("[OrderController]---applyOrder--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 后台新增订单
     * @param applyOrderQo
     * @return
     */
    @PostMapping("/addOrder")
    public PlatformResult addOrder(@Validated @RequestBody ApplyOrderQo applyOrderQo){
        try {
            orderWebService.addOrder(applyOrderQo);
        } catch (BusinessException be) {
            log.error("[OrderController]---addOrder--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        }catch (Exception e) {
            log.error("[OrderController]---addOrder--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }



    /**
     * 订单确认收货
     * @param confirmReceiptQo
     * @return
     */
    @PostMapping("/confirmReceipt")
    public PlatformResult confirmReceipt(@Validated @RequestBody ConfirmReceiptQo confirmReceiptQo){
        try {
            orderWebService.confirmReceipt(confirmReceiptQo.getOrderId());
        } catch (BusinessException e) {
            log.error("[OrderController]---confirmReceipt--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---confirmReceipt--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
        return PlatformResult.success();
    }


    /**
     * 小程序登录用户页面 订单列表查询
     * @param orderListQo
     * @return
     */
    @PostMapping("/orderList")
    public PlatformResult orderList(@RequestBody OrderListQo orderListQo){
        try {
            return PlatformResult.success(orderWebService.orderList(orderListQo));
        } catch (BusinessException e) {
            log.error("[OrderController]---orderList--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---orderList--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }

    /**
     * 订单审核
     * @param auditOrderQo
     * @return
     */
    @PostMapping("/auditOrder")
    public PlatformResult auditOrder(@Validated @RequestBody AuditOrderQo auditOrderQo){
        try {
            orderWebService.auditOrder(auditOrderQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[OrderController]---auditOrder--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---auditOrder--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }

    /**
     * 修改订单码段
     * @param editOrderQo
     * @return
     */
    @PostMapping("/editOrder")
    public PlatformResult editOrder(@Validated @RequestBody EditOrderQo editOrderQo){
        try {
            orderWebService.editOrder(editOrderQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[OrderController]---editOrder--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---editOrder--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }
    /**
     * 订单确认收款
     * @param confirmReceiptQo
     * @return
     */
    @PostMapping("/confirmCollection")
    public PlatformResult confirmCollection(@Validated @RequestBody ConfirmReceiptQo confirmReceiptQo){
        try {
            orderWebService.confirmCollection(confirmReceiptQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[OrderController]---confirmCollection--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---confirmCollection--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }


    /**
     * 订单列表后台查询
     * @param orderListQo
     * @return
     */
    @PostMapping("/orderListAdmin")
    public PlatformResult orderListAdmin(@RequestBody OrderListQo orderListQo){
        try {
            return PlatformResult.success(orderWebService.orderListAdmin(orderListQo));
        } catch (BusinessException e) {
            log.error("[OrderController]---orderListAdmin--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---orderListAdmin--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }

    /**
     * 根据条件获取所有订单  不分页
     * @param orderListQo
     * @return
     */
    @PostMapping("/orderAllList")
    public PlatformResult orderAllList(@RequestBody OrderListQo orderListQo){
        try {
            return PlatformResult.success(orderWebService.orderAllList(orderListQo));
        } catch (BusinessException e) {
            log.error("[OrderController]---orderAllList--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---orderAllList--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }


    /**
     * 添加订单备注
     * @param addOrderRemarkQo
     * @return
     */
    @PostMapping("/addOrderRemark")
    public PlatformResult addOrderRemark(@Validated @RequestBody AddOrderRemarkQo addOrderRemarkQo){
        try {
            orderWebService.addOrderRemark(addOrderRemarkQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[OrderController]---addOrderRemark--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---addOrderRemark--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/orderDetail/{orderId}")
    public PlatformResult orderDetail(@PathVariable Long orderId){
        try {
            return PlatformResult.success(orderWebService.orderDetail(orderId));
        } catch (BusinessException e) {
            log.error("[OrderController]---orderDetail--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---orderDetail--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }

    /**
     * 订单废弃
     * @param
     * @return
     */
    @GetMapping("/discardOrder/{orderId}/{remark}")
    public PlatformResult discardOrder(@PathVariable Long orderId,@PathVariable String remark){
        try {
            orderWebService.discardOrder(orderId,remark);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[OrderController]---discardOrder--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---discardOrder--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }
}
